/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.hslu.prg2.team2.gui;

import ch.hslu.prg2.team2.logic.ComputerPlayer;
import java.awt.GridLayout;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Claudio
 */
public class GameboardSizeDialog extends JDialog
{
    private IGameboardSizeChanged gameboardSizeChangedListener;
    private JTextField rowsField;
    private JTextField colsField;
    private int initialRows;
    private int initialCols;
    
    public GameboardSizeDialog(JFrame owner, int rows, int cols)
    {
        super(owner);
        
        initialRows = rows;
        initialCols = cols;
        
        JLabel rowsLabel = new JLabel("Zeilen");
        JLabel colsLabel = new JLabel("Spalten");               
        
        rowsField = new JTextField(Integer.toString(rows));
        colsField = new JTextField(Integer.toString(cols));
        
        JButton buttonOk = new JButton("OK");
        buttonOk.addActionListener(new ButtonOkListener());
        buttonOk.setSize(80, 26 );
        
        JButton buttonAbort = new JButton("Abbrechen");
        buttonAbort.addActionListener(new ButtonAbortListener());
        buttonAbort.setSize(80, 26 );
        
        getContentPane().setLayout(new GridLayout(3,2));
        getContentPane().add(rowsLabel);
        getContentPane().add(rowsField);
        getContentPane().add(colsLabel);
        getContentPane().add(colsField);
        getContentPane().add(buttonOk);
        getContentPane().add(buttonAbort);
        
        setTitle("Spielfeldgrösse anpassen");
        setSize(240,120);
        setResizable(false);
        setLocation(500,300);
    }
    
    public void setGameboardSizeChangedListener( IGameboardSizeChanged gameboardSizeChangedListener )
    {
        this.gameboardSizeChangedListener = gameboardSizeChangedListener;
    }
    
    public class ButtonOkListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            if( gameboardSizeChangedListener != null )
            {
                int rows = initialRows;
                int cols = initialCols;
                
                try  
                {  
                    rows = Integer.parseInt(rowsField.getText());  
                } 
                catch(NumberFormatException nfe)  
                {  
                    JOptionPane.showConfirmDialog(null, "Im Feld Zeilen werden nur Zahlenwerte akzeptiert.", "Falsche Eingabe", JOptionPane.DEFAULT_OPTION, 2);
                    return;
                }
                
                try
                {
                    cols = Integer.parseInt(colsField.getText());
                }
                catch(NumberFormatException nfe)
                {
                    JOptionPane.showConfirmDialog(null, "Im Feld Spalten werden nur Zahlenwerte akzeptiert.", "Falsche Eingabe", JOptionPane.DEFAULT_OPTION, 2);
                    return;
                }
                
                if( rows < 5 || cols < 5 || rows > 15 || cols > 15 )
                {
                    JOptionPane.showConfirmDialog(null, "Es werden nur Werte zwischen 5 und 15 akzeptiert..", "Falsche Eingabe", JOptionPane.DEFAULT_OPTION, 2);
                    return;
                }
                
                gameboardSizeChangedListener.gameboardSizeChanged(rows, cols);
            }
            setVisible(false);
        }        
    }
    
    public class ButtonAbortListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            setVisible(false);
        }
    }
}
